package command;

import java.io.File;

import org.codehaus.plexus.util.cli.CommandLineException;
import org.codehaus.plexus.util.cli.CommandLineUtils;
import org.codehaus.plexus.util.cli.Commandline;

public class PlexusCommandLine {
	
	private int exitCode;
	
	private Commandline commandline = new Commandline();
	
	public PlexusCommandLine(String command) {
		commandline.setExecutable(command);
  }
	
	public void setWorkingDirectory(String dir){
		commandline.setWorkingDirectory(new File(dir));
	}
	
	public int getExitCode() {
	  return exitCode;
  }
	
	public String execute(String... args) {
		commandline.addArguments(args);

		CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
		CommandLineUtils.StringStreamConsumer out = new CommandLineUtils.StringStreamConsumer();

		try {
			exitCode = CommandLineUtils.executeCommandLine(commandline, out, err);			
		} catch (CommandLineException e) {
			e.printStackTrace();
		}

		String output = out.getOutput();

		String error = err.getOutput();
		
		return output + error;
	}
}
